package cn.etgps.etgps.mvp;


import android.app.Activity;

import java.util.HashMap;

import cn.etgps.etgps.base.BasePresenter;
import cn.etgps.etgps.dataEntity.BaseBean;
import cn.etgps.etgps.dataEntity.VehicleDetailBean;
import cn.etgps.etgps.dataEntity.VehicleInfoBean;
import cn.etgps.etgps.event.EventExpenseEntity;
import cn.etgps.etgps.event.EventOilLogEntitiy;
import cn.etgps.etgps.event.EventReceiveEntity;
import cn.etgps.etgps.event.EventSendEntity;
import cn.etgps.etgps.utils.Constant;
import cn.etgps.etgps.utils.T;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by itte on 2016/3/28.
 */
public class Traffic_Presenter extends BasePresenter<Traffic_Iview> {
    public Traffic_Presenter(Activity context, Traffic_Iview view) {
        super(context, view);
    }

    /** 获取详情 **/
    public void onGetVehicleDetail(String vid) {
        getParams = new HashMap<>(0);
        getParams.put("Vid", vid);
        httpApi.getVehicleDetailedInfo(getParams)
                .subscribeOn(io.reactivex.schedulers.Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<VehicleDetailBean>() {
                    @Override
                    public void accept(VehicleDetailBean vehicleDetailBean) throws Exception {
                        iView.dismissNormalDialog();
                        if (vehicleDetailBean != null) {
                            if (Constant.RETURN_RESULT_OK.equals(vehicleDetailBean.getStatus())) {
                                iView.onBindDetail(vehicleDetailBean.getContent());
                            } else {
                                T.showShort(mContext, vehicleDetailBean.getErrorMsg());
                            }
                        }
                    }
                }, new Consumer<Throwable>() {

                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        iView.dismissNormalDialog();
                        T.showShort(mContext, throwable.getMessage());
                    }
                });
    }

    /** 上传记录 **/
    public void onSaveVehicleRunLog(String vid, String vehicleTypeName, String driverName, String distance,
            EventSendEntity sendEntity,
            EventReceiveEntity receiveEntity,
            EventOilLogEntitiy oilLogEntitiy,
            EventExpenseEntity eventExpenseEntity) {
        getParams = new HashMap<>(0);
        getParams.put("Vid", vid);
        getParams.put("TypeName", vehicleTypeName);
        getParams.put("DriverName", driverName);
        getParams.put("RunDistance", distance);
        if (sendEntity != null) {
            getParams.put("OutTime", sendEntity.getDate());
            getParams.put("OutPositon", sendEntity.getAddress());
            getParams.put("OutDistance", sendEntity.getKmil());
        }
        if (receiveEntity != null) {
            getParams.put("BackTime", receiveEntity.getDate());
            getParams.put("BackPostion", receiveEntity.getAddress());
            getParams.put("BackDistance", receiveEntity.getKmil());
        }
        if (oilLogEntitiy != null) {
            getParams.put("OilType", oilLogEntitiy.getOilType());
            getParams.put("OilNumber", oilLogEntitiy.getOilCard());
            getParams.put("OilCount", oilLogEntitiy.getOilCount());
            getParams.put("OilDistance", oilLogEntitiy.getOilDistance());
            getParams.put("OilDate", oilLogEntitiy.getOilDate());
            getParams.put("OilPrice", oilLogEntitiy.getOilPrice());
            getParams.put("OilACount", oilLogEntitiy.getOilAddPrice());
           /* getParams.put("Mpg", oilLogEntitiy.getOilMpg());*/
        }
        if (eventExpenseEntity != null) {
            getParams.put("HstxCount", eventExpenseEntity.getHighPrice());
            getParams.put("HsNotes", eventExpenseEntity.getHighSheet());
            getParams.put("StopCount", eventExpenseEntity.getParkPrcie());
            getParams.put("StopNotes", eventExpenseEntity.getParkSheet());
            getParams.put("Washing", eventExpenseEntity.getWashPrice());
            getParams.put("WashingNotes", eventExpenseEntity.getWashSheet());
        }
        httpApi.saveRunlog(getParams)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseBean>() {
                    @Override
                    public void accept(BaseBean baseBean) throws Exception {
                        iView.dismissNormalDialog();
                        if (baseBean != null) {
                            if (Constant.RETURN_RESULT_OK.equals(baseBean.getStatus())) {
                                iView.onNext();
                            } else {
                                T.showShort(mContext, baseBean.getErrorMsg());
                            }
                        }

                    }
                }, new Consumer<Throwable>() {

                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        iView.dismissNormalDialog();
                    }
                });

    }

    /** 得到车辆列表 **/
    public void onRequestVehicleInfo(final boolean isRefresh, int pageIndex, int pageSize) {
        getParams = new HashMap<>(0);
        getParams.put("userId", userId);
        getParams.put("userType", userType);
        getParams.put("pageIndex", String.valueOf(pageIndex));
        getParams.put("pageSize", String.valueOf(pageSize));
        httpApi.getVehicleInfo(getParams)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<VehicleInfoBean>() {
                    @Override
                    public void accept(VehicleInfoBean vehicleInfoBean) throws Exception {
                        iView.onRefreshComplete(isRefresh);
                        if (vehicleInfoBean != null && Constant.RETURN_RESULT_OK.equals(vehicleInfoBean.getStatus())) {
                            iView.onBindData(isRefresh, vehicleInfoBean.getContent());
                        }

                    }
                }, new Consumer<Throwable>() {

                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        iView.onRefreshComplete(isRefresh);
                    }
                });
    }
}
